package com.likeycy.my;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @ClassName: TimeTool
 * @Description: 统计方法运行时间的小工具
 * @Author: sodagreen
 * @Date: 2021/2/20 1:38
 * @Version: 1.0
 */
public class TimeTool {

    private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSS");

    public interface Task {
        void execute();
    }

    public static void check(String title, Task task) {
        if (task == null) {
            return;
        }
        title = (title == null) ? "" : ("【" + title + "】");
        System.out.println(title);
        System.out.println("开始：" + DATE_TIME_FORMATTER.format(LocalDateTime.now()));
        long begin = System.currentTimeMillis();
        task.execute();
        long end = System.currentTimeMillis();
        System.out.println("结束：" + DATE_TIME_FORMATTER.format(LocalDateTime.now()));
        double delta = (end - begin) / 1000.0;
        System.out.println("耗时：" + delta + "秒");
        System.out.println("----------------------------------------------------------");

    }
}
